package designPattern.command.undoCommand
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	public class InvokerPanel extends Sprite
	{
		private var commandList:Array;
		private var btnList:Array;
		
		public function InvokerPanel()
		{
			super();
			commandList = [];
			btnList = [];
		}
		
		public function setCommandBtn(nSlot:int, btnName:String, command:CommandWithUndo):void
		{
			commandList[nSlot] = command;
			
			//创建command对应的btn,并加入到btnList中
			var textBtn:Sprite = new Sprite();
			btnList[nSlot] = textBtn;
			textBtn.graphics.beginFill(0x88FF33);
			textBtn.graphics.drawRect(0, 0, 20, 20);
			textBtn.graphics.endFill();
			var deTxt:TextField = new TextField();
			deTxt.text = btnName;
			deTxt.mouseEnabled = false;
			textBtn.addChild(deTxt);
			addChild(textBtn);
			textBtn.x = 100 * nSlot;
			textBtn.y = 100;
			textBtn.addEventListener(MouseEvent.CLICK, clickHandler);
		}
		
		private function clickHandler(evt:MouseEvent):void
		{
			for(var i:int = 0; i < btnList.length; i++)
			{
				if(evt.target == btnList[i])
				{
					(commandList[i] as CommandWithUndo).execute();
					break;
				}
			}
		}
	}
}